package com.ceshiren.hogwarts.restassured.wechat.apiobject.framwork.testcase;

/**
 * @author Seven
 * @date 2020/12/31 10:32
 */

import com.ceshiren.hogwarts.restassured.wechat.apiobject.framwork.steps.StepModel;
import com.ceshiren.hogwarts.restassured.wechat.apiobject.framwork.steps.StepResult;
import com.ceshiren.hogwarts.restassured.wechat.apiobject.framwork.test.Test02_ApiObjectModelTest;
import com.ceshiren.hogwarts.restassured.wechat.utils.FakerUtils;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.dataformat.yaml.YAMLFactory;
import org.junit.jupiter.api.function.Executable;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;

import static org.junit.jupiter.api.Assertions.assertAll;

/**
 * 用例yaml对应的数据对象
 */
public class ApiTestCaseModel {
    //将yaml对应的变量定义
    private String name;
    private String  description;
    private ArrayList<StepModel> steps;

    private ArrayList<Executable> assertList = new ArrayList<>();
    private HashMap<String,String> testCaseVaribales = new HashMap<>();
    public static final Logger logger = LoggerFactory.getLogger(Test02_ApiObjectModelTest.class);


    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public ArrayList<StepModel> getSteps() {
        return steps;
    }

    public void setSteps(ArrayList<StepModel> steps) {
        this.steps = steps;
    }

    public ArrayList<Executable> getAssertList() {
        return assertList;
    }

    public void setAssertList(ArrayList<Executable> assertList) {
        this.assertList = assertList;
    }

    public HashMap<String, String> getTestCaseVaribales() {
        return testCaseVaribales;
    }

    public void setTestCaseVaribales(HashMap<String, String> testCaseVaribales) {
        this.testCaseVaribales = testCaseVaribales;
    }

    public static ApiTestCaseModel load(String path) throws IOException {
        ObjectMapper objectMapper = new ObjectMapper(new YAMLFactory());
        return objectMapper.readValue(new File(path),ApiTestCaseModel.class);
    }

    public void run(){
        /**
         * 1.加载用例层关键字变量
         */
        this.testCaseVaribales.put("getTimeStamp", FakerUtils.getTimeStamp());
        logger.info("用例变量更新："+testCaseVaribales);
        /**
         * 2.遍历step进行执行
         */
        steps.forEach(step->{
            StepResult stepResult = step.run(testCaseVaribales);
            /**
             * 3.处理step返回的save变量进行追加
             */
            if (stepResult.getStepVariables().size()>0){
                //putall()另外一个map放到另一个map，进行合并和追加
                testCaseVaribales.putAll(stepResult.getStepVariables());
                logger.info("testcase变量更新："+testCaseVaribales);
            }
            /**
             * 4.处理assertList,并进行统一断言
             */
            if(stepResult.getArrayList().size()>0){
                assertList.addAll(stepResult.getArrayList());
            }
        });

        /**
         * 5.软断言
         */
        assertAll("",assertList.stream());
    }
}
